//
//  MSBeatufulDIYVC.m
//  MS
//
//  Created by Yuan on 13-2-14.
//  Copyright (c) 2013年 He Yuan. All rights reserved.
//

#import "MSBeatufulDIYVC.h"
#import "MSBeatufulDIYView.h"
#import "MSDIYDetailVC.h"

@interface MSBeatufulDIYVC ()
{
    NSMutableArray *diydatas;
    
    UIScrollView *diyScrollView;
}

@property(nonatomic,retain)NSMutableArray *diydatas;

@property(nonatomic,retain)UIScrollView *diyScrollView;

@end

@implementation MSBeatufulDIYVC
@synthesize diydatas;
@synthesize diyScrollView;

- (id)init
{
    self = [super init];
    if (self) {
        diydatas = [[NSMutableArray alloc]init];
    }
    return self;
}

-(void)loadView
{
    [super loadView];
    
    diyScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    [diyScrollView setBackgroundColor:[UIColor clearColor]];
    [diyScrollView setPagingEnabled:YES];
    [self.view addSubview:diyScrollView];
}


-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setTitle:HYLocalizedString(@"扮美DIY") showBackButton:YES];
    
    [self loadDIYData];
}

- (void)configureViews:(BOOL)animation
{
    [super configureViews:animation];
    
}

-(void)loadDIYData
{
    NSDictionary *p =  [NSDictionary dictionaryWithObjectsAndKeys:
                        [NSNumber numberWithInt:1],@"specialty",
                        [NSNumber numberWithInt:1],@"page",
                        nil];
    [WebService  getRequestAction:APIDIYList
                            param:p
                          success:^(NSDictionary *info,NSError *error)
     {
         if (error == nil)
         {
             HYLog(@"%@",info);
             if (info && [info isKindOfClass:[NSDictionary class]])
             {
                 [diydatas removeAllObjects];
                 [diydatas addObjectsFromArray:[info objectForKey:@"result"]];
                 [self refreshUI];
             }
         }
         [MSTool showError:error];
     } failure:^(id info ,NSError *error){
         [MSTool showError:error];
     } progress:self.navigationController.view];
}

-(void)refreshUI
{
    UIImage *bgImage = [UIImage imageNamed:@"diy_item_bg"];

    for (int i = 0 ; i < [diydatas count]; i++)
    {
        CGRect rect = CGRectMake(18.5 + i*320,diyScrollView.frame.size.height/2 - bgImage.size.height/2 - 24,bgImage.size.width,bgImage.size.height);
        MSBeatufulDIYView *v = [[MSBeatufulDIYView alloc]initWithFrame:rect info:[diydatas objectAtIndex:i] delegate:self tag:i];
        [diyScrollView addSubview:v];
    }
    [diyScrollView setContentSize:CGSizeMake(3*320, 0)];
}


-(void)viewDetail:(UIGestureRecognizer *)g
{
    MSDIYDetailVC *vc = [[MSDIYDetailVC alloc]initWithInfo:[diydatas objectAtIndex:g.view.tag]];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
